using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Data.Common;
using MagnoliaTableAdapters;

public partial class SignIn : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Page.Title = ConfigurationManager.AppSettings["PageTitle"] + " - SignIn";
        
        string WelcomeText = "Welcome to the " + ConfigurationManager.AppSettings["PageTitle"] + "<br />";
        WelcomeText += "Content Management Web Application";
        WelcomeText += "<br /><br />";
        WelcomeText += "Please Sign In Below";
        Lbl_Welcome.Text = WelcomeText;

        TextBox_UserName.Focus();

        if (PreviousPage != null)
        {
            if (PreviousPage.Title.Contains("ABC SuperSign"))
            {
                // this is a logout, so end session
                Session.Abandon();
            }
        }

    }
    protected void Btn_SignIn_Click(object sender, EventArgs e)
    {
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["SuperSignConnectionString"].ConnectionString);
        conn.Open();

        string sProc = "sproc_WEB_AuthenticateUser";

        SqlCommand cmd = new SqlCommand(sProc, conn);
        cmd.CommandType = CommandType.StoredProcedure;

        // set paramter
        DbParameter param = cmd.CreateParameter();
        param.ParameterName = "@UserName";
        param.Value = Convert.ToString(TextBox_UserName.Text);
        param.DbType = DbType.String;
        cmd.Parameters.Add(param);

        // set paramter
        param = cmd.CreateParameter();
        param.ParameterName = "@Password";
        param.Value = Convert.ToString(TextBox_Password.Text);
        param.DbType = DbType.String;
        cmd.Parameters.Add(param);

        // execute the stored procedure and return the results
        int Result = Convert.ToInt32(cmd.ExecuteScalar());

        if (Result > 0)
        {
            // Create session variables for user id, user access, session id
            Session["UserID"] = Result;
            Magnolia.USERTYPEACCESSDataTable UserAccess = new Magnolia.USERTYPEACCESSDataTable();
            USERTYPEACCESSTableAdapter UserAccessAdapter = new USERTYPEACCESSTableAdapter();
            UserAccessAdapter.FillByUserID(UserAccess, Result);
            Session["UserAccess"] = UserAccess;
            Application["SessionCount"] = Convert.ToInt32(Application["SessionCount"]) + 1;
            Session["SessionID"] = Application["SessionCount"];
            Session.Timeout = 60;  //NOW SET TO 20 BY WEB SERVER
            Response.Redirect("ScheduleList.aspx");
        }
        else
        {
            Label_Alert.Text = "Username or password is incorrect.  Please try again.";
            Label_Alert.Style.Add("color","Red");
            Label_Alert.Style.Add("font-weight","bold");
            Label_Alert.Style.Add("margin", "0");
            Label_Alert.Style.Add("padding", "0");
            Label_Alert.Style.Add("height", "20px");
            Label_Alert.Style.Add("font-size", "11px");
            Label_Alert.Enabled = true;
        }
    }
}
